package com.youoor.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youoor.common.IdWorker;
import com.youoor.common.base.BaseDao;
import com.youoor.common.base.BaseServiceImpl;
import com.youoor.dao.CardFavoDao;
import com.youoor.domain.CardFavo;
import com.youoor.domain.CardFavoQuery;
import com.youoor.domain.CardFavoQuery.Criteria;
import com.youoor.service.CardFavoService;

@Service(value="cardFavoService")
public class CardFavoServiceImpl extends BaseServiceImpl<CardFavo,CardFavoQuery,Long> implements CardFavoService {

	private static final Logger logger = LoggerFactory.getLogger(CardFavoServiceImpl.class);
	
	@Resource(name = "cardFavoDao")
	private CardFavoDao cardFavoDao;
	
	public CardFavoDao getCardFavoDao() {
		return cardFavoDao;
	}

	public void setCardFavoDao(CardFavoDao cardFavoDao) {
		this.cardFavoDao = cardFavoDao;
	}

	@Autowired
	private IdWorker idWorker;
	
	@Override
	public BaseDao<CardFavo, CardFavoQuery, Long> getBaseDao() {
		// TODO Auto-generated method stub
		return cardFavoDao;
	}

	@Override
	public CardFavo verifyFavo(int cardId, int favoCardId) {
		// TODO Auto-generated method stub
		return cardFavoDao.verifyFavo(cardId, favoCardId);
	}

	@Override
	public void insertCardFavo(int cardId, int favoCardId) {
		Date nowTime = new Date();
		CardFavo cardFavo = new CardFavo();
		cardFavo.setCardId(cardId);
		cardFavo.setFavoCardId(favoCardId);
		cardFavo.setFavoId(idWorker.nextId());
		cardFavo.setCtime(nowTime);
		cardFavo.setUtime(nowTime);
		cardFavoDao.insertCardFavo(cardFavo);
		
		
	}

	@Override
	public List<CardFavo> getFavoList(int favoCardId,int pageIndex,int num) {
		CardFavoQuery query = new  CardFavoQuery();
		query.setOrderByClause("ctime desc");
		Criteria criteria = query.createCriteria();
		criteria.andFavoCardIdEqualTo(favoCardId);
		return  cardFavoDao.selectByExamplePage(query,pageIndex*num, num);
	}

	@Override
	public List<CardFavo> getFavoListToMe(int cardId,Long favoId,int num) {
		CardFavoQuery query = new  CardFavoQuery();
		query.setOrderByClause("ctime desc");
		Criteria criteria = query.createCriteria();
		if(favoId != -1) {
			criteria.andCardIdEqualTo(cardId).andFavoIdLessThan(favoId);
		}else {
			criteria.andCardIdEqualTo(cardId);
		}
		return  cardFavoDao.selectByExample(query, num);
	}


}
